package com.nextive.weightlogger;

/**
 * The constants of this class represent weight units.
 * 
 * It provides methods to convert weight values from and to grams.
 * 
 * @author Federico Baseggio <fedebaseggio@gmail.com>
 */
public enum WeightUnit {
	POUND ("lbs", 0, 454),
	KILOGRAM ("kg", 1, 1000);
	
	/**
	 * International symbol for this weight unit.
	 */
	private String symbol;
	
	/**
	 * Index of this unit in the string-array resource.
	 */
	private int resourceItemIndex;
	
	/**
	 * The amount of grams equivalent to a unit of this measure unit.
	 */
	private long gramsPerUnit;
	
	/**
	 * Constructor.
	 * 
	 * @param symbol
	 *            International symbol for this weight unit.
	 * @param resourceItemIndex
	 *            Index of this unit in the string-array resource.
	 * @param gramsPerUnit
	 *            The amount of grams equivalent to a unit of this measure unit.
	 */
	private WeightUnit(String symbol, int resourceItemIndex, long gramsPerUnit) {
		this.symbol = symbol;
		this.resourceItemIndex = resourceItemIndex;
		this.gramsPerUnit = gramsPerUnit;
	}

	/**
	 * Index of this unit in the string-array resource.
	 * 
	 * @return The index of this unit in the string-array resource.
	 */
	public int getResourceItemIndex() {
		return resourceItemIndex;
	}

	/**
	 * International symbol for this weight unit.
	 * 
	 * @return The international symbol for this weight unit.
	 */
	public String getSymbol() {
		return symbol;
	}
	
	/**
	 * Converts a value from this unit to grams.
	 * 
	 * @param value The value expressed in this unit.
	 * @return The equivalent value in grams.
	 */
	public float toGrams(float value){
		return value * gramsPerUnit;
	}
	
	/**
	 * Converts a value from grams to this unit.
	 * 
	 * @param value The value expressed in grams.
	 * @return The equivalent value in this unit.
	 */
	public float valueFromGrams(float value){
		return  value / gramsPerUnit;
	}
}
